class DevicesController < ApplicationController
  def index
    @devices = Device.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @devices }
    end
  end
  
  def show
    @device = Device.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @device }
    end
  end
  
  
  def new
    @device = Device.new
    @device.device_features.build
  end

  def create
    @device = Device.new(params[:device])
    if @device.save
      flash[:notice] = "Successfully created device and features."
      @device.marketValue
      redirect_to devices_path
    else
       render :action => 'new'
    end
  end

def edit
  @device = Device.find(params[:id])
end

def update
  params[:device][:existing_device_feature_attributes] ||= {}
  
  @device = Device.find(params[:id])
  if @device.update_attributes(params[:device])
    @device.marketValue
    flash[:notice] = "Successfully updated device and features."
    redirect_to device_path(@device)
  else
    render :action => 'edit'
  end
end

 def destroy
    @device = Device.find(params[:id])
    @device.destroy

    respond_to do |format|
      format.html { redirect_to(devices_url) }
      format.xml  { head :ok }
    end
  end
  
 def add_new_roll
   @device=Device.find(params[:id])
   @roll=params[:roll]
   @device.add_roll(@roll.to_i)
 end
 
 def updatecost
   @device=Device.find(params[:id])
   @features=Feature.find :all
 end

end
